package cn.xspace.wnx.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.xspace.android.xafcommon.util.ResourceUtils;
import com.xspace.android.xafcommon.view.RoundAngleImageView;

import java.util.List;

import cn.xspace.wnx.R;
import cn.xspace.wnx.entity.NewFriendBean;

/**
 * Wubj 创建于 2017/2/8 0008.
 */
public class NewFriendsAdapter extends BaseAdapter {

    private static final String TAG = NewFriendsAdapter.class.getSimpleName();

    private Context mContext;
    private List<NewFriendBean> dataList;

    public NewFriendsAdapter(Context context, List<NewFriendBean> dataList) {
        this.mContext = context;
        this.dataList = dataList;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public NewFriendBean getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NewFriendsViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_new_friend, null);
            holder = new NewFriendsViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (NewFriendsViewHolder) convertView.getTag();
        }
        holder.bindData(getItem(position), position);
        return convertView;
    }

    public class NewFriendsViewHolder {
        View rootView;
        RoundAngleImageView mImageViewHead;
        TextView mTvTitle;
        TextView mTvContent;
        RelativeLayout mTlTitle;
        RelativeLayout mRlLeft;
        RelativeLayout mRelativeBg;
        TextView btConfirmFriend;

        public NewFriendsViewHolder(View rootView) {
            this.rootView = rootView;
            this.mImageViewHead = (RoundAngleImageView) rootView.findViewById(R.id.imageview_head);
            this.mTvTitle = (TextView) rootView.findViewById(R.id.tv_title);
            this.mTvContent = (TextView) rootView.findViewById(R.id.tv_content);
            this.mTlTitle = (RelativeLayout) rootView.findViewById(R.id.tl_title);
            this.mRlLeft = (RelativeLayout) rootView.findViewById(R.id.rl_left);
            this.mRelativeBg = (RelativeLayout) rootView.findViewById(R.id.relative_bg);
            this.btConfirmFriend = (TextView) rootView.findViewById(R.id.bt_confirm_friend);
        }

        public void bindData(final NewFriendBean bean, final int position) {
            Picasso.with(mContext)
                    .load(bean.user_logo)
                    .placeholder(R.drawable.icon_header_default)
                    .error(R.drawable.icon_header_default)
                    .into(mImageViewHead);
            mTvTitle.setText(bean.user_name);
            mTvContent.setText(bean.req_desc);
            if (bean.add_status == 1) { //已添加
                btConfirmFriend.setClickable(false); //不可点击
                btConfirmFriend.setText(R.string.already_add);
                btConfirmFriend.setTextColor(ResourceUtils.getColor(R.color.app_text_normal4));
                btConfirmFriend.setBackgroundResource(R.color.transparent);
            } else {
                btConfirmFriend.setClickable(true);
                btConfirmFriend.setText(R.string.confirm);
                btConfirmFriend.setTextColor(ResourceUtils.getColor(R.color.white));
                btConfirmFriend.setBackgroundResource(R.drawable.shape_bg_bt_blue);
                btConfirmFriend.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null) {
                            listener.onConfirmClick(bean, position);
                        }
                    }
                });
            }

        }
    }

    public IConfirmClickListener listener;

    public void setConfirmClickListener(IConfirmClickListener listener) {
        this.listener = listener;
    }

    public interface IConfirmClickListener {
        void onConfirmClick(NewFriendBean bean, int position);
    }

}
